    function doClickEvent(obj){
	   if (document.all){//IE
           obj.fireEvent('onclick');
       }else{//FF
           var evt = document.createEvent("MouseEvents");
           evt.initEvent("click", true, true);
           obj.dispatchEvent(evt);
       }
    }
	function doSubmit(event){
	    event=event?event:(window.event?window.event:null);
		if (event.keyCode==13){
           doClickEvent(document.getElementById("loginButton"));
		}
		return;
	}
	function trimAll(data){
	  var reg=/^ +| +$/g;
	  var str=data.replace(reg,"");
	  return str;
	}
	function checkInput(){
		if(trimAll(document.getElementById("loginName").value)==""){
			Ext.Msg.alert('提示','用户名不能为空！');
			document.getElementById("loginName").focus();
			return false;
		}else if(trimAll(document.getElementById("password").value)==""){
			Ext.Msg.alert('提示','密码不能为空！');
			document.getElementById("password").focus();
			return false;
		}else if(trimAll(document.getElementById("chkcode").value)==""){
			Ext.Msg.alert('提示','验证码不能为空！');
			return false;
		}
		return true;
	}
	function doNextInput(event,obj){
		event=event?event:(window.event?window.event:null);//IE/FF
		if (event.keyCode==13){
			obj.focus();
		}
		return;
	}
	
    Ext.get('loginButton').on('click', function(){
      if(checkInput()){
	       Ext.Msg.wait('正在验证，请稍候...','提示');
	       var loginName = document.getElementById("loginName").value; 
           var password = document.getElementById("password").value;
           var chkcode = document.getElementById("chkcode").value;
           setCookie('loginName', loginName);
           Ext.Ajax.request({
   				url : cfg.cxt+'/user/logining.action',
   				method : 'POST',
   				success : function(response) {
   					 var r = Ext.decode(response.responseText);
   					 if(r.success){
   						if (r.status==200) {
							document.location='main.action';
							return;
						}else if(r.status==401) {
							Ext.Msg.hide();
							Ext.Msg.alert('系统消息','&nbsp;&nbsp;&nbsp;&nbsp;验证码错误或已过期,请重新输入&nbsp;&nbsp;&nbsp;&nbsp;');
						} else if(r.msg=='hasLogin') {
							Ext.Msg.hide();
							Ext.Msg.alert('系统消息','&nbsp;&nbsp;&nbsp;&nbsp;您已经登录了此系统，请退出系统后登录其他账户！&nbsp;&nbsp;&nbsp;&nbsp;',function(){
								  window.location  ="main.do";
							});
						} else {//error
							Ext.Msg.hide();
							Ext.Msg.alert('系统消息','&nbsp;&nbsp;&nbsp;&nbsp;'+r.info+'&nbsp;&nbsp;&nbsp;&nbsp;');
						}
					    document.getElementById("checkImg").setAttribute("src",cfg.cxt+"/servlet/RandomCodeServlet?"+Math.round(Math.random()*10000));	
   					 } else {
   						Ext.Msg.alert('系统消息','服务器出错！');
   					}
   				},
   				failure : function(response){
   					console.log(response);
   					Ext.Msg.hide();
					Ext.Msg.alert('系统消息','服务器出错！');
   				},
   				params : {
   					'loginName' : loginName,
   					'password' : password,
   					'chkcode'  : chkcode
   				}
			});
       }
    });
    //记住用户名
    function setCookie(name, value) {//两个参数，一个是cookie的名子，一个是值
        var expireDay = 30; //此 cookie 将被保存 30 天
        var exp = new Date();
        exp.setTime(exp.getTime() + expireDay*24*60*60*1000);
        document.cookie = name + "="+ escape(value) + ";expires=" + exp.toGMTString();
    }
    function getCookie(name) {
        var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
        if(arr != null) {
        	return unescape(arr[2]);
        }
        return null;
    }
    function delCookie(name) {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval= getCookie(name);
        if(cval != null) {
        	document.cookie= name + "=" + cval + ";expires=" + exp.toGMTString();
        }
    }
	//获取默认用户
    var loginName = getCookie("loginName");
    if (loginName != null && loginName.length > 0){
    	document.getElementById("loginName").value = loginName;
    	document.getElementById("password").focus();
    } else {
    	 document.getElementById("loginName").focus();
    }
